package org.budo.elasticsearch.repository.spring.data;

import org.budo.elasticsearch.repository.BudoElasticsearchRepository;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.SearchQuery;

import lombok.Getter;
import lombok.Setter;

/**
 * @author limingwei
 */
@Getter
@Setter
public class SpringDataElasticsearchTemplateRepository implements BudoElasticsearchRepository {
    private ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public <T> Page<T> scroll(String scrollId, long scrollTimeInMillis, SearchResultMapper mapper) {
        return this.getElasticsearchTemplate().scroll(scrollId, scrollTimeInMillis, mapper);
    }

    @Override
    public void clearScroll(String scrollId) {
        this.getElasticsearchTemplate().clearScroll(scrollId);
    }

    @Override
    public String scan(SearchQuery searchQuery, long scrollTimeInMillis, boolean noFields) {
        return this.getElasticsearchTemplate().scan(searchQuery, scrollTimeInMillis, noFields);
    }

    @Override
    public void setSearchTimeout(String searchTimeout) {
        this.getElasticsearchTemplate().setSearchTimeout(searchTimeout);
    }

    @Override
    public <T> AggregatedPage<T> queryForPage(SearchQuery searchQuery, Class<T> clazz, SearchResultMapper mapper) {
        return this.getElasticsearchTemplate().queryForPage(searchQuery, clazz, mapper);
    }

    @Override
    public SearchRequestBuilder prepareSearch(String... indices) {
        return this.getElasticsearchTemplate().getClient().prepareSearch(indices);
    }
}